<template>
    <div>
        <div style="width: 100%;">
            <el-button type="primary" @click="dialogFormVisible = true; operataType = 'save'"
                style="width: 150px;margin-bottom: 20px; height: 40px;">添加商品类型</el-button>
            <el-input placeholder="请输入内容" v-model="name"  style="width: 300px; left: 15px"
            suffix-icon="el-icon-search" @keyup.enter.native="findByName">
                <el-button slot="append" icon="el-icon-search" @click="findByName"></el-button>
            </el-input>
        </div>
        <el-dialog title="商品信息" :visible.sync="dialogFormVisible" @close="handleClose">
            <el-form :model="productTypes" ref="productTypes" label-width="100px" class="demo-dynamic">
                <el-form-item label="商品编号" :label-width="formLabelWidth" prop="id" :rules="[
                    { required: true, message: '商品编号不能为空' }
                ]">
                    <el-input v-model="productTypes.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="分类名称" :label-width="formLabelWidth" prop="product_type_name" :rules="[
                    { required: true, message: '分类名称不能为空' }
                ]">
                    <el-input v-model="productTypes.product_type_name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="分类描述" :label-width="formLabelWidth" prop="product_type_desc" :rules="[
                    { required: true, message: '分类描述不能为空' }
                ]">
                    <el-input v-model="productTypes.product_type_desc" autocomplete="off"></el-input>
                </el-form-item>
                <!-- <el-form-item label="分类图标" :label-width="formLabelWidth">
                    <el-input v-model="productTypes.product_type_icon" autocomplete="off"></el-input>
                </el-form-item> -->
                <el-form-item label="分类图标" :label-width="formLabelWidth">
                    <el-upload class="avatar-uploader" action="http://localhost:8080/upload" :show-file-list="false"
                        :on-success="handleAvatarSuccess">
                        <el-image style="width: 100px; height: 100px" v-if="productTypes.product_type_icon"
                            :src="productTypes.product_type_icon">
                        </el-image>
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave('dynamicValidateForm')">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 数据表格 -->
        <el-table :data="productType" style="width: 100%">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="商品编号" width="180">
                <template slot-scope="scope">
                    <td>{{ scope.row.id }}</td>
                </template>
            </el-table-column>
            <el-table-column label="分类名称" width="180">
                <template slot-scope="scope">
                    <td>{{ scope.row.product_type_name }}</td>
                </template>
            </el-table-column>
            <el-table-column label="分类描述" width="180">
                <template slot-scope="scope">
                    <td>{{ scope.row.product_type_desc }}</td>
                </template>
            </el-table-column>
            <!-- <el-table-column label="分类图标" width="180">
                <template slot-scope="scope">
                    <td>{{ scope.row.product_type_icon }}</td>
                </template>
            </el-table-column> -->
            <el-table-column label="分类图标" width="180">
                <template slot-scope="scope">
                    <el-image style="width: 100px; height: 100px" :src="scope.row.product_type_icon"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize"
            :current-page="current" @current-change="loadProducts">
        </el-pagination>
    </div>
</template>
<script>
export default {
    name: "ProductTypeView",
    data() {
        return {
            productType: [],
            dialogFormVisible: false,
            formLabelWidth: '120px',
            name: "",
            productTypes: {
                id: '',
                product_type_name: '',
                product_type_desc: '',
                product_type_icon: '',
            },
            operataType: '',
            total: 0,
            pageSize: 5,
            current: 0,
            productTypes: {
                id: ''
            }
        }
    },
    methods: {
        //读取商品
        loadProducts(current) {
            this.current = current
            //调用后台的方法，参数为URL
            this.$http.get('/product_type?action=page&current=' + this.current + "&pageSize=" + this.pageSize)
                .then(resp => {
                    //处理返回结果
                    console.log(resp.data)
                    //数据绑定
                    if (resp.data.data) {
                        this.productType = resp.data.data.records
                        this.total = resp.data.data.total
                    }
                })
        },
        handleEdit(index, row) {
            this.operataType = 'update';
            //显示对话框，绑定数据
            this.dialogFormVisible = true;
            this.productTypes = JSON.parse(JSON.stringify(row))
        },
        handleSave(formName) {
            // this.$refs[formName].validate((valid) => {
            //     if (valid) {
            //         alert('submit!');
            //     } else {
            //         console.log('error submit!!');
            //         return false;
            //     }
            // });
            if (this.operataType == 'save') {
                //添加操作
                console.log('save')
                this.$http.post('/product_type?action=save', this.$qs.stringify(this.productTypes))
                    .then(resp => {
                        console.log("save")
                        if (resp.data.data == true) {
                            this.loadProducts(this.current)
                        }
                    })
            } else {
                //更新
                console.log('update')
                this.$http.post('/product_type?action=update', this.$qs.stringify(this.productTypes))
                    .then(resp => {
                        console.log("update")
                        if (resp.data.data == true) {
                            this.loadProducts(this.current)
                        }
                    })
            }
            this.dialogFormVisible = false
        },
        handleDelete(index, row) {
            this.$confirm('是否确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/product_type?action=delete&id=' + row.id).then(resp => {
                    if (resp.data.data == true) {
                        this.loadProducts(this.current);
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //根据分类名称查询商品
        findByName() {
            this.$http.post('/product_type?action=findByNameContaining&name=' + this.name).then(resp => {
                if(resp.data.data) {
                    this.total = resp.data.data.length
                    this.productType = resp.data.data
                    this.$message({
                        type: 'success',
                        message: '查询成功!'
                    });
                }
            })
        },
        //关闭对话框
        handleClose() {
            this.productTypes = {
                id: '',
                product_type_name: '',
                product_type_desc: '',
                product_type_icon: '',
            };
            this.operataType = ''
        },
        handleAvatarSuccess(res, file) {
            console.log('upload', res.data)
            this.productTypes.product_type_icon = res.data
        }
    },
    //页面挂载钩子
    mounted() {
        this.loadProducts(1)
    }
}
</script>
